package com.seashop.util;

import com.seashop.response.ResponseBase;
import com.seashop.constant.BaseConstant;
import com.seashop.enums.CountryCodeEnum;

/**
 * @author moredln
 * @desc 电话号码校验工具
 * @date 2020-07-10 22:44
 */
public class CheckMobileUtil {

    // 66泰国
    private static final String TH = "66";
    // 62印尼
    private static final String ID = "62";
    // 86中国
    private static final String CN = "86";
    // 1美国
    private static final String US = "1";
    // 03巴基斯坦
    private static final String PK = "03";

    public static ResponseBase checkMobile(String mobile, String areaCode) {
        if (!areaCode.startsWith(TH) && !areaCode.startsWith(ID) && !areaCode.startsWith(CN) && !areaCode.startsWith(PK)) {
            // 66泰国 62印尼
            return ResponseBase.fail(BaseConstant.AREA_CODE_ERROR);
        }
        try {
            Long.valueOf(mobile);
        } catch (Exception e) {
            return ResponseBase.fail(BaseConstant.MOBILE_FORMAT_ERROR);
        }
        if (areaCode.startsWith(TH)) {
            if (!mobile.startsWith("06") && !mobile.startsWith("08") && !mobile.startsWith("09")) {
                return ResponseBase.fail(BaseConstant.MOBILE_FORMAT_ERROR);
            }
            if (mobile.length() != 10) {
                return ResponseBase.fail(BaseConstant.MOBILE_FORMAT_ERROR);
            }
        } else if (areaCode.startsWith(ID)) {
            if (!mobile.startsWith("08")) {
                return ResponseBase.fail(BaseConstant.MOBILE_FORMAT_ERROR);
            }
            if (mobile.length() < 10 || mobile.length() > 13) {
                return ResponseBase.fail(BaseConstant.MOBILE_FORMAT_ERROR);
            }
        }
        return null;
    }

    public static String areaCodeByCountryCode(String countryCode) {
        if (countryCode.equals(CountryCodeEnum.TH.name())) {
            return TH;
        } else if (countryCode.equals(CountryCodeEnum.ID.name())) {
            return ID;
        } else if (countryCode.equals(CountryCodeEnum.CN.name())) {
            return CN;
        } else if (countryCode.equals(CountryCodeEnum.US.name())) {
            return US;
        } else if (countryCode.equals(CountryCodeEnum.PK.name())) {
            return PK;
        }
        return null;
    }

}
